Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a vitest unit test for the Rules Engine Python whl #109

Merged
merged 12 commits into from
Nov 30, 2023
Merged

Conversation

thadk
Copy link
Member

@thadk thadk commented Nov 22, 2023

From the heat-stack folder, run npm test app/utils/pyodide.test.ts or see the new whole heat-stack Checks below.

  • npm run test should pre-run the necessary python build commands, not too sure about dependencies (see package.json)
  • created vitest unit test of pyodide module based on Epic Stack pro workshop testing solution in misc.error-message.test.ts
  • tweaked path of pyodide-env in test
    • from /pyodide-env for browser
    • to public/pyodide-env for vitest environment
  • used engine.hdd(57, 60) but we may ultimately want to use an alternative version of the test_rules_engine/*.py files
  • bumped minor pyodide version
  • fixed up linter to avoid errors, including .eslintignore
  • create the Github Action automation
  • disabled Epic Stack's deploy and playwright GitHub actions:
    • Deploy not needed yet
    • and Playwright is underutilized yet, slow, and jank-failed 1 out of 20 runs.
  • modify python Github Action to target v3.11.3 (matches Pyodide 0.24.1).
  • rebased to latest main and added new type definition requirements.txt to test and public/pyodide-env folder to make pass again.

Full output:


➜ npm test app/utils/pyodide.test.ts
zsh: correct 'test' to 'tests' [nyae]? n

> test
> pushd ../rules-engine && python3 -m venv venv && source venv/bin/activate && pip install -q build && python3 -m build && popd && vitest app/utils/pyodide.test.ts

~/gitrepos/home-energy-analysis-tool/rules-engine ~/gitrepos/home-energy-analysis-tool/heat-stack

[notice] A new release of pip is available: 23.2.1 -> 23.3.1
[notice] To update, run: pip install --upgrade pip
* Creating venv isolated environment...
* Installing packages in isolated environment... (setuptools)
* Getting build dependencies for sdist...
running egg_info
writing src/rules_engine.egg-info/PKG-INFO
writing dependency_links to src/rules_engine.egg-info/dependency_links.txt
writing requirements to src/rules_engine.egg-info/requires.txt
writing top-level names to src/rules_engine.egg-info/top_level.txt
reading manifest file 'src/rules_engine.egg-info/SOURCES.txt'
writing manifest file 'src/rules_engine.egg-info/SOURCES.txt'
* Building sdist...
running sdist
running egg_info
writing src/rules_engine.egg-info/PKG-INFO
writing dependency_links to src/rules_engine.egg-info/dependency_links.txt
writing requirements to src/rules_engine.egg-info/requires.txt
writing top-level names to src/rules_engine.egg-info/top_level.txt
reading manifest file 'src/rules_engine.egg-info/SOURCES.txt'
writing manifest file 'src/rules_engine.egg-info/SOURCES.txt'
running check
creating rules-engine-0.0.1
creating rules-engine-0.0.1/src
creating rules-engine-0.0.1/src/rules_engine
creating rules-engine-0.0.1/src/rules_engine.egg-info
copying files to rules-engine-0.0.1...
copying README.md -> rules-engine-0.0.1
copying pyproject.toml -> rules-engine-0.0.1
copying src/rules_engine/__init__.py -> rules-engine-0.0.1/src/rules_engine
copying src/rules_engine/engine.py -> rules-engine-0.0.1/src/rules_engine
copying src/rules_engine.egg-info/PKG-INFO -> rules-engine-0.0.1/src/rules_engine.egg-info
copying src/rules_engine.egg-info/SOURCES.txt -> rules-engine-0.0.1/src/rules_engine.egg-info
copying src/rules_engine.egg-info/dependency_links.txt -> rules-engine-0.0.1/src/rules_engine.egg-info
copying src/rules_engine.egg-info/requires.txt -> rules-engine-0.0.1/src/rules_engine.egg-info
copying src/rules_engine.egg-info/top_level.txt -> rules-engine-0.0.1/src/rules_engine.egg-info
copying src/rules_engine.egg-info/SOURCES.txt -> rules-engine-0.0.1/src/rules_engine.egg-info
Writing rules-engine-0.0.1/setup.cfg
Creating tar archive
removing 'rules-engine-0.0.1' (and everything under it)
* Building wheel from sdist
* Creating venv isolated environment...
* Installing packages in isolated environment... (setuptools)
* Getting build dependencies for wheel...
running egg_info
writing src/rules_engine.egg-info/PKG-INFO
writing dependency_links to src/rules_engine.egg-info/dependency_links.txt
writing requirements to src/rules_engine.egg-info/requires.txt
writing top-level names to src/rules_engine.egg-info/top_level.txt
reading manifest file 'src/rules_engine.egg-info/SOURCES.txt'
writing manifest file 'src/rules_engine.egg-info/SOURCES.txt'
* Installing packages in isolated environment... (wheel)
* Building wheel...
running bdist_wheel
running build
running build_py
creating build
creating build/lib
creating build/lib/rules_engine
copying src/rules_engine/__init__.py -> build/lib/rules_engine
copying src/rules_engine/engine.py -> build/lib/rules_engine
running egg_info
writing src/rules_engine.egg-info/PKG-INFO
writing dependency_links to src/rules_engine.egg-info/dependency_links.txt
writing requirements to src/rules_engine.egg-info/requires.txt
writing top-level names to src/rules_engine.egg-info/top_level.txt
reading manifest file 'src/rules_engine.egg-info/SOURCES.txt'
writing manifest file 'src/rules_engine.egg-info/SOURCES.txt'
installing to build/bdist.macosx-13-arm64/wheel
running install
running install_lib
creating build/bdist.macosx-13-arm64
creating build/bdist.macosx-13-arm64/wheel
creating build/bdist.macosx-13-arm64/wheel/rules_engine
copying build/lib/rules_engine/__init__.py -> build/bdist.macosx-13-arm64/wheel/rules_engine
copying build/lib/rules_engine/engine.py -> build/bdist.macosx-13-arm64/wheel/rules_engine
running install_egg_info
Copying src/rules_engine.egg-info to build/bdist.macosx-13-arm64/wheel/rules_engine-0.0.1-py3.11.egg-info
running install_scripts
creating build/bdist.macosx-13-arm64/wheel/rules_engine-0.0.1.dist-info/WHEEL
creating '/Users/thadk/gitrepos/home-energy-analysis-tool/rules-engine/dist/.tmp-k18bytp9/rules_engine-0.0.1-py3-none-any.whl' and adding 'build/bdist.macosx-13-arm64/wheel' to it
adding 'rules_engine/__init__.py'
adding 'rules_engine/engine.py'
adding 'rules_engine-0.0.1.dist-info/METADATA'
adding 'rules_engine-0.0.1.dist-info/WHEEL'
adding 'rules_engine-0.0.1.dist-info/top_level.txt'
adding 'rules_engine-0.0.1.dist-info/RECORD'
removing build/bdist.macosx-13-arm64/wheel
Successfully built rules-engine-0.0.1.tar.gz and rules_engine-0.0.1-py3-none-any.whl
~/gitrepos/home-energy-analysis-tool/heat-stack

 DEV  v0.34.4 /Users/thadk/gitrepos/home-energy-analysis-tool/heat-stack

stdout | app/utils/pyodide.test.ts > pyodide loads
Loading numpy

stdout | app/utils/pyodide.test.ts > pyodide loads
Loaded numpy
Loading rules_engine

stdout | app/utils/pyodide.test.ts > pyodide loads
Loaded rules_engine

 ✓ app/utils/pyodide.test.ts (1) 1667ms
   ✓ pyodide loads 1665ms

 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  01:59:23
   Duration  2.21s (transform 70ms, setup 387ms, collect 4ms, tests 1.67s, environment 0ms, prepare 41ms)

Test output:

Intentional fail:

image

Pass:

image

@thadk thadk requested a review from alanisaac November 22, 2023 07:05
@thadk thadk marked this pull request as draft November 22, 2023 07:10
@thadk

This comment was marked as outdated.

@thadk thadk marked this pull request as ready for review November 22, 2023 15:33
@thadk
Copy link
Member Author

thadk commented Nov 22, 2023

@alanisaac Welcome to review. I also changed the Github Action python target version to 3.11.3 which is the current pyodide release's version .

The main next step I can think of is moving the rules_engine-0.0.1-py3-none-any.whl file out of the rules-engine/dist folder into the heat-stack/public/pyodide-env folder so it can be used by the frontend too and not just in the test, but we'll get to that soon. It's just a little tweak.

@thadk thadk requested a review from plocket November 22, 2023 17:17
@thadk thadk added rules-engine Issues related to the Python rules engine heat-app-js Issues related to the HEAT app frontend labels Nov 22, 2023
Copy link
Collaborator

@alanisaac alanisaac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice in public/pyodide-env that it looks like there's a binary related to the Python dependencies used (like numpy). Are those meant to be committed to source control?

@thadk
Copy link
Member Author

thadk commented Nov 24, 2023

@alanisaac Yep, that folder re-hosts locally:

  • the python environment binary WebAssembly files, copied from the Pyodide node_modules library folder
  • and whl files (like for numpy library) which I manually extracted from the Pyodide 0.24.1 ~212mb release pyodide-0.24.1.tar.bz2.
    • This is mostly for running offline. If it can't find any library you try to access, pyodide will also try to download them from the default content delivery network (cdn).

Both of these binaries have to be fetched at Javascript run-time and aren't bundleable along with the Pyodide Javascript library itself.

@thadk
Copy link
Member Author

thadk commented Nov 24, 2023

used engine.hdd(57, 60) but we may ultimately want to use an alternative version of the test_rules_engine/*.py files

I tried to run pytest and the test_engine.py from within the Pyodide environment. I executed that file contents in python within Pyodide, then ran pytest.main() but python crashed. Pytest seems very file oriented and pyodide is not too much.

Based on that, If we want an internal Pyodide test like test_engine.py, we might need to wrap those assertions with some other library than pytest. On ChatGPT or on StackOverflow, I saw unittest suggested but I'm not at all sure if that would be the right choice.

More broadly, if we want to run test_examples.py in Pyodide, it might need to be made into a whl or part of the main whl because of its directory structure.

@jkoren
Copy link
Collaborator

jkoren commented Nov 29, 2023

Thad will:

  • Revert root.tsx to remove changes made by linter (removed ; and changed ' to ")
  • Modify linter to give warnings but not make changes as above

@plocket
Copy link
Collaborator

plocket commented Nov 29, 2023

Also, just to be sure, close the block comment at https://github.com/codeforboston/home-energy-analysis-tool/pull/109/files#diff-ae03e02fefef517ad9aa21f08290622cf90b06ccfb95f94362b273a43a70aa9eR16. [It's fine for now, but if we remove the in-line commenting, it'll be a problem.]

@thadk thadk merged commit 0b6f2d1 into main Nov 30, 2023
11 checks passed
AdamFinkle pushed a commit to AdamFinkle/home-energy-analysis-tool that referenced this pull request May 22, 2024
…oston#109)

* add pyodide rules_engine whl test with vitest
* bump pyodide to 0.24.1 from Sept 25
* add test github action for frontend test, fix linter
* change python target ver, adjust gha names
* make eslint warn no-unused-vars
* rebase & add pydantic typing deps to pyodide unit test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
heat-app-js Issues related to the HEAT app frontend rules-engine Issues related to the Python rules engine
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants